// 2pages/login/login.js
let app = getApp()
let request = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paddingTop: app.globalData.paddingTop,
    userName: '',
    userVerify: '',
    verifyText: '获取验证码',
    isGetVerify: false,
    isAcquire: false,
    verifyTime: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let token = wx.getStorageSync('token')
    if (token) {
      wx.redirectTo({
        url: '/pages/index/index',
      })
      return
    }
  },
  /**
   * 获取用户名
   * @param {object} event
   */
  getUserName(e) {
    let val = e.detail.value
    if (val.length >= 11) {
      val = val.slice(0,11)
    }
    this.setData({
      userName: val
    })
  },
  /**
   * 获取验证码
   * @param {object} event
   */
  getUserVerify(e) {
    let val = e.detail.value
    this.setData({
      userVerify: val
    })
  },
  /**
   * 获取验证码
   */
  bindGetVerify() {
    if (this.data.isGetVerify) {
      return
    }
    let moblieReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
    let userName = this.data.userName
    if (!moblieReg.test(userName)) {
      wx.showToast({
        title: '请您填写正确手机号',
        icon: 'none'
      })
      return
    }
    let timer = null;
    let i = this.data.verifyTime
    let that = this
    let parmas = {
      phoneNumber: userName
    }
    request.http('/park/park-manager/get-verify-code', 'GET', parmas)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            verifyText: i + '秒后重新获取',
            isGetVerify: true,
            isAcquire: true
          })
          timer = setInterval(function () {
            i--
            if (i <= 0) {
              clearInterval(timer)
              that.setData({
                verifyText: '获取验证码',
                isGetVerify: false
              })
              return
            }
            that.setData({
              verifyText: i + '秒后重新获取',
              isGetVerify: true
            })
          }, 1000)
        }
      })
  },
  /**
   * 登录按钮
   */
  bindLogin() {
    let moblieReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
    let userName = this.data.userName
    let userVerify = this.data.userVerify
    if (!moblieReg.test(userName)) {
      wx.showToast({
        title: '请您填写正确手机号',
        icon: 'none'
      })
      return
    }
    if (!userVerify) {
      wx.showToast({
        title: '请您填写验证码',
        icon: 'none'
      })
      return
    }
    if (!this.data.isAcquire) {
      wx.showToast({
        title: '请先获取验证码',
        icon: 'none'
      })
      return
    }
    let parmas = {
      phoneNumber: userName,
      verifyCode: userVerify
    }
    request.http('/park/park-manager/login', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          let token = data.data.token
          wx.setStorageSync('token', token)
          wx.setStorageSync('parkId', data.data.parkId)
          wx.reLaunch({
            url: '/pages/index/index',
          })
        }
      })
  }
})